package com.slz3.business.module.wmsManagement.wmsStockTransfer.service;

import com.slz3.business.frame.module.Repositorys;
import com.slz3.business.frame.util.VE;
import com.slz3.business.module.wmsManagement.wmsStockDetail.domain.WmsStockDetail;
import com.slz3.business.module.wmsManagement.wmsStockDetail.domain.WmsStockDetailFactory;
import com.slz3.business.module.wmsManagement.wmsStockTransfer.domain.WmsStockTransfer;
import com.slz3.business.module.wmsManagement.wmsStockTransfer.domain.WmsStockTransferFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class WmsStockTransferTransactionalServiceImpl implements WmsStockTransferTransactionalService {

    @Override
    public WmsStockTransfer save(WmsStockTransfer wmsStockTransfer) {
        WmsStockTransfer wmsStockTranferNew = Repositorys.wmsStockTransfer.save(WmsStockTransferFactory.initBaseFields(wmsStockTransfer));
        if(VE.isNotEmpty(wmsStockTransfer.getStockDetailList())){
            List<WmsStockDetail>  stockDetailList = wmsStockTransfer.getStockDetailList();
            stockDetailList.stream().forEach(stockDetail ->{
                stockDetail.setUpper(wmsStockTranferNew.getUuid());
                WmsStockDetailFactory.initBaseFields(stockDetail);
            });
            Repositorys.wmsStockDetail.saveAll(stockDetailList);
        }
        return wmsStockTranferNew;
    }

    @Override
    public WmsStockTransfer modify(WmsStockTransfer wmsStockTransferNew) {
        Repositorys.wmsStockTransfer.test();
        Repositorys.wmsStockTransfer.save(WmsStockTransferFactory.initBaseFields());
        System.err.println(1 / 0);
        wmsStockTransferNew.setLastModifyTime(LocalDateTime.now());
        return Repositorys.wmsStockTransfer.saveAndFlush(wmsStockTransferNew);
    }

    @Override
    public List<WmsStockTransfer> batchModify(List<WmsStockTransfer> wmsStockTransferList) {
        return Repositorys.wmsStockTransfer.saveAll(wmsStockTransferList);
    }
}
